#import "Common.h"
#import "DataType.h"
#import "LayerMainMenu.h"
#import "LayerSelMode.h"
#import "LayerOption.h"
#import "LayerHelp.h"
#import "Particle.h"
#import "OpenFeint/OpenFeint.h"

NSString* sMenuBtnFile[8] = {
	@"btn-play.png", @"btn-option.png", @"btn-help.png", @"btn-leaderboard.png", @"btn-achievement.png",
	@"btn-OF.png", @"btn-facebook.png", @"btn-twitter.png"
};
CGPoint sMenuItemPos[8] = {
	ccp(240,80), ccp(440,40), ccp(400,40), ccp(40,40), ccp(80,40), ccp(120,40), ccp(400,290), ccp(440,290)
};

@implementation SceneMainMenu
-(id) init
{
	if ((self = [super init]))
		[self addChild:[LayerMainMenu node]];
	return self;
}
@end

@implementation LayerMainMenu

-(id) init
{
	if((self = [super init])) {	
		CCSprite* bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = gPointCenter;
		[self addChild:bg];
	
		[bg runAction:
		 [CCRepeatForever actionWithAction:
		  [CCSequence actions:
		   [CCTintTo actionWithDuration:4 red:255 green:50 blue:50],
		   [CCTintTo actionWithDuration:4 red:255 green:255 blue:255],			 
		   [CCTintTo actionWithDuration:4 red:50 green:255 blue:50],
		   [CCTintTo actionWithDuration:4 red:255 green:255 blue:255],			 
		   NULL]]]; 

		ParticleSpace* par = [ParticleSpace node];
		[self addChild:par];
	
		CCSprite* logo = [CCSprite spriteWithFile:@"gamelogo.png"];
		logo.position = ccp(240,200);
		[self addChild:logo];
		
		//menu
		CCMenu* menu = [CCMenu menuWithItems:NULL];
		menu.position = CGPointZero;
		[self addChild:menu];
		
		CCSprite* item;
		CCSprite* itemDown;
		CCMenuItemSprite* menuItem;
		
		for(int i=0; i<8; i++) {
			item = [CCSprite spriteWithFile:sMenuBtnFile[i]];
			itemDown = [CCSprite spriteWithFile:sMenuBtnFile[i]];
			itemDown.color = ccGRAY;
			menuItem = [CCMenuItemSprite itemFromNormalSprite:item selectedSprite:itemDown
																								 target:self selector:@selector(callMenu:)];
			menuItem.position = sMenuItemPos[i];
			if(i == 0) {
				[menuItem runAction:
				 [CCRepeatForever actionWithAction:
					[CCSequence actions:
					 [CCScaleTo actionWithDuration:0.1 scale:1.1],
					 [CCScaleTo actionWithDuration:0.1 scale:0.9],
					 [CCScaleTo actionWithDuration:0.1 scale:1.05],
					 [CCScaleTo actionWithDuration:0.1 scale:1],
					 [CCDelayTime actionWithDuration:0.3], NULL]]];
			}
			[menu addChild:menuItem z:i+1000];
		}
		
//		CCLOG(@"LayerMainMenu init finish");
	}
	return self;
}

-(void) callMenu:(CCMenuItem*)sender
{
	int id = [sender zOrder];

	[[SimpleAudioEngine sharedEngine] playEffect:SOUND_BTN01];
	
	switch(id) {
		case 1000:		// play
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneSelMode node]]];
			break;
		case 1001:		// option
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneOption node]]];
			break;
		case 1002:		// help
			[[CCDirector sharedDirector] replaceScene:
			 [CCTransitionFade transitionWithDuration:0.5 scene:[SceneHelp node]]];
			break;
		case 1003:		// leaderboard
			[gGCManager showLeaderboard:NULL];
			break;
		case 1004:		// achievement
			[gGCManager showAchievements];
			break;
		case 1005:		// openfeint
			[OpenFeint launchDashboard];
			break;
		case 1006:		// facebook
		{
			NSString* str = @"http://m.facebook.com/pages/LIHO-games/226052067412667";
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
			break;
		}
		case 1007:		// twitter
		{
			NSString* str = @"http://mobile.twitter.com/Lihogames";
			[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
			break;
		}
	}
}

@end
